//var chk_value_str = "";//用于保存已选择的商品，
$(function () {
    var activityId = $("#activityId").val();

    //var form = window.form_edit_activity;
    //扫码区域是否受限
    /*if($("#isLimitScanRegion_edit").val() == 0){
     $("#isLimitScanRegion_edit").attr("checked",true);
     }else{
     $("#isLimitScanRegion_edit").attr("checked",false);
     }*/
    //设置渠道选中
    var str = $("#channel_edit").val();
    if (str.length > 0) {
        var channelStr = "商超,酒店,名烟名酒行,传统批发";
        $(str.split(",")).each(function (i, dom) {
            $("[name='channel'][value='" + dom + "']").prop("checked", true);
            if (channelStr.indexOf(dom) < 0) {
                $("#chk_custom").attr("checked", true);
                $("#channel_custom").val(dom);
            }
        });
    }
    //加载已存在的抽奖背景图
    $("#backgroundImage1").empty();
    var image1 = $("#backgroundImage1").val();
    if (image1) {
        var pictures = image1.split(",");
        for (var i in pictures) {
            var uri = pictures[i];
            if (/.+/.test(uri)) {
                var $imgEle = $("<img src='" + PicDomain + uri + "' data-picuri='" + uri + "'/>");
                $imgEle.click(function () {
                    var _self = $(this);
                    bootbox.bconfirm("确定要删除这个背景图吗？", function (re) {
                        if (re) {
                            _self.remove();
                            $("#ctn_edit_product_pics").siblings("[name=btn_new_preview]").removeData("img");
                        }
                    });

                });
                //加载默认的抽奖背景图

                $("#ctn_edit_product_pics").append($imgEle).siblings("[name=btn_new_preview]").data("img", PicDomain + uri);
            }
        }
    }
    //加载已存在的中奖背景图
    $("#backgroundImageWin").empty();
    var image_win = $("#backgroundImageWin").val();
    if (image_win) {
        var pictures = image_win.split(",");
        for (var i in pictures) {
            var uri = pictures[i];
            if (/.+/.test(uri)) {
                var $imgEle = $("<img src='" + PicDomain + uri + "' data-picuri='" + uri + "'/>");
                $imgEle.click(function () {
                    var _self = $(this);
                    bootbox.bconfirm("确定要删除这个背景图吗？", function (re) {
                        if (re) {
                            _self.remove();
                            $("#ctn_edit_product_pics_win").siblings("[name=btn_new_preview]").removeData("img");
                        }
                    });
                });
                $("#ctn_edit_product_pics_win").append($imgEle).siblings("[name=btn_new_preview]").data("img", PicDomain + uri);
                ;
            }
        }
    }
    //加载已存在的未中奖背景图
    $("#backgroundImageLos").empty();
    var image_nowin = $("#backgroundImageLos").val();
    if (image_nowin) {
        var pictures = image_nowin.split(",");
        for (var i in pictures) {
            var uri = pictures[i];
            if (/.+/.test(uri)) {
                var $imgEle = $("<img src='" + PicDomain + uri + "' data-picuri='" + uri + "'/>");
                $imgEle.click(function () {
                    var _self = $(this);
                    bootbox.bconfirm("确定要删除这个背景图吗？", function (re) {
                        if (re) {
                            _self.remove();
                            $("#ctn_edit_product_pics_nowin").siblings("[name=btn_new_preview]").removeData("img");
                        }
                    });
                });
                $("#ctn_edit_product_pics_nowin").append($imgEle).siblings("[name=btn_new_preview]").data("img", PicDomain + uri);
                ;
            }
        }
    }
    //活动编辑页面，活动日期选择;默认编辑页面显示已保存的活动时间
    // var star = $("#actTimeStart").val();
    // var end = $("#actTimeEnd").val();
    // $("#activity_start_edit").val(star);
    // $("#activity_end_edit").val(end);

    $('#activity_start_edit').daterangepicker({
    	"singleDatePicker": true,
		"timePicker": true,
		"timePicker24Hour": true,
		"timePickerSeconds": true,
		"parentEl": "200",
		
		locale: {
		  format:'YYYY-MM-DD HH:mm:ss',
	      applyLabel: '确认',
	      cancelLabel: '取消',
	      weekLabel: 'W',
	      customRangeLabel: 'Custom Range',
	      daysOfWeek:["日","一","二","三","四","五","六"],
	      monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
		}
    }, function (start, end, label) {
        $('#activity_start_edit').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });
    
    $('#activity_end_edit').daterangepicker({
    	"singleDatePicker": true,
		"timePicker": true,
		"timePicker24Hour": true,
		"timePickerSeconds": true,
		"parentEl": "200",
		
		locale: {
		  format:'YYYY-MM-DD HH:mm:ss',
	      applyLabel: '确认',
	      cancelLabel: '取消',
	      weekLabel: 'W',
	      customRangeLabel: 'Custom Range',
	      daysOfWeek:["日","一","二","三","四","五","六"],
	      monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
		}
    }, function (start, end, label) {
        $('#activity_end_edit').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });


    //上传抽奖背景图片
    $("[name='btn_edit_product_upload_pic']").click(function () {
        var uploaded_count = $("#ctn_edit_product_pics").find("img").length;
        if (uploaded_count < 1) {
            $("#ipt_edit_product_upload_pic").click();
        } else {
            bootbox.balert("抽奖背景图只能上传1张图片");
            return;
        }
    });
    $('#ipt_edit_product_upload_pic').fileupload({
        dataType: 'json',
        url: "/upload/pic?type=3",
        done: function (e, data) {
            var res = data.jqXHR.responseJSON;
            var $imgEle = $("<img src='" + PicDomain + res.data + "' data-picuri='" + res.data + "'/>");
            $imgEle.click(function () {
                $(this).remove();
                $("#ctn_edit_product_pics").siblings("[name=btn_new_preview]").removeData("img");
            });
            $("#ctn_edit_product_pics").append($imgEle).siblings("[name=btn_new_preview]").data("img", PicDomain + res.data);
            ;
        }
    });

    //上传中奖背景图片
    $("[name='btn_edit_product_upload_pic_win']").click(function () {
        var uploaded_count = $("#ctn_edit_product_pics_win").find("img").length;
        if (uploaded_count < 1) {
            $("#ipt_edit_product_upload_pic_win").click();
        } else {
            bootbox.balert("中奖背景图只能上传1张图片");
            return;
        }
    });
    $('#ipt_edit_product_upload_pic_win').fileupload({
        dataType: 'json',
        url: "/upload/pic?type=3",
        done: function (e, data) {
            var res = data.jqXHR.responseJSON;
            var $imgEle = $("<img src='" + PicDomain + res.data + "' data-picuri='" + res.data + "'/>");
            $imgEle.click(function () {
                $(this).remove();
                $("#ctn_edit_product_pics_win").siblings("[name=btn_new_preview]").removeData("img");
            });
            $("#ctn_edit_product_pics_win").append($imgEle).siblings("[name=btn_new_preview]").data("img", PicDomain + res.data);
        }
    });
    //上传未中奖背景图片
    $("[name='btn_edit_product_upload_pic_nowin']").click(function () {
    	console.log("--------");
        var uploaded_count = $("#ctn_edit_product_pics_nowin").find("img").length;
        if (uploaded_count < 1) {
            $("#ipt_edit_product_upload_pic_nowin").click();
        } else {
            bootbox.balert("中奖背景图只能上传1张图片");
            return;
        }
    });
    $('#ipt_edit_product_upload_pic_nowin').fileupload({
        dataType: 'json',
        url: "/upload/pic?type=3",
        done: function (e, data) {
            var res = data.jqXHR.responseJSON;
            var $imgEle = $("<img src='" + PicDomain + res.data + "' data-picuri='" + res.data + "'/>");
            $imgEle.click(function () {
            	bootbox.bconfirm("确定要删除中奖背景图吗？删除后将显示默认图片", function (re) {
					if(re){
						$imgEle.remove();
						$("#ctn_edit_product_pics_nowin").siblings(".btn-default").removeData("img");
					}
            	});
            });
            $("#ctn_edit_product_pics_nowin").append($imgEle).siblings(".btn-default").data("img", PicDomain + res.data);
        }
    });

    
    //自动抽奖操作
    var temp = $("#isAutoDraw_hidden").val();
    $("[name='isAutoDraw'][value='" + temp + "']").prop("checked", "checked");
    if(temp == 1){
    	$("#get_div").hide();
    }else{
    	$("#get_div").show();
    }

});

//删除区域
function btnAction(re) {
    $(re).parent().parent().remove();
}

//删除活动与区域关联
$("[name='btn_edit_activity_delRgn']").click(function () {
    var activityRegionId = $(this).data("rgn-id");
    $.delete("/activity/delRgn/" + activityRegionId, function (re) {
        if (re.data == 1) {
            window.location.href = "/activity?activityId=" + $("#activityId").val();
        } else {
            //tipper.danger("删除失败，请重试");
        }
    });
});

//取消活动
$("[name='btn_cancel_edit_activity']").click(function(){
	window.location.href="/activity";
});




//===================以下为公共方法============================
/**
 *转换long值为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */

function getFormatDateByLong(l, pattern) {
    return getFormatDate(new Date(l), pattern);
}
/**
 *转换日期对象为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    }
    return date.format(pattern);
}
//扩展Date的format方法   
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
